#include "mkListener.h"

mkListener::mkListener(){}

mkListener::~mkListener(void){}

//********************************************************************************************
// initializes DirectInput
//
//********************************************************************************************
void mkListener::initDInput(HINSTANCE hInstance, HWND hWnd)
{
    // create the DirectInput interface
    DirectInput8Create(hInstance,
                       DIRECTINPUT_VERSION,    // the compatible version
                       IID_IDirectInput8,      // the DirectInput interface version
                       (void**)&this->din,     // the pointer to the interface
                       NULL);

    // create the keyboard device
    din->CreateDevice(GUID_SysKeyboard,
                      &this->dinkeyboard,      // the pointer to the device interface
                      NULL);
    din->CreateDevice(GUID_SysMouse,
                      &this->dinmouse,         // the pointer to the device interface
                      NULL);

    // set the data format to keyboard format
    this->dinkeyboard->SetDataFormat(&c_dfDIKeyboard);
    this->dinmouse->SetDataFormat(&c_dfDIMouse);

    // set the control we will have over the keyboard
    this->dinkeyboard->SetCooperativeLevel(hWnd, 
										   DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
    this->dinmouse->SetCooperativeLevel(hWnd, 
										DISCL_EXCLUSIVE | DISCL_FOREGROUND);
}

//********************************************************************************************
// gets the latest input data
//
//********************************************************************************************
void mkListener::detect_input(void)
{
    // get access if we don't have it already
    this->dinkeyboard->Acquire();
    this->dinmouse->Acquire();

    // get the input data
    this->dinkeyboard->GetDeviceState(256, (LPVOID)keystate);
    this->dinmouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mousestate);
}

//********************************************************************************************
// closes DirectInput
//
//********************************************************************************************
void mkListener::cleanDInput(void)
{
    this->dinkeyboard->Unacquire();    // make sure the keyboard is unacquired
    this->dinmouse->Unacquire();    // make sure the mouse in unacquired
    this->din->Release();    // close DirectInput before exiting
}

//********************************************************************************************
// gets the mouse state
//
//********************************************************************************************
DIMOUSESTATE mkListener::getMouseState()
{
	return this->mousestate;
}

//********************************************************************************************
// gets the keyboard state
//
//********************************************************************************************
BYTE* mkListener::getKeyState()
{
	return this->keystate;
}
